local CommonOHOH = _G.CommonOHOH;

local M = CommonOHOH.class.new(script, CommonOHOH.files.CommonObject());
local PlayerCharacterHelper = _G.CommonOHOH.load.PlayerCharacterHelper();

local BroCharacterClass=require(script.Parent.Character)

local playerAtt=_G.ClientOHOH:GetPath_Loop("BroShow.root.player",workspace)
local gBroShowCframe=playerAtt.WorldCFrame
local gBroHideCframe=playerAtt.Hide.WorldCFrame

local gFolder=Instance.new("Folder",playerAtt)
gFolder.Name="bro"

local gCharacter={}
local gLoadingCharacter={}

function CreateCharacter(userId)
    if gCharacter[userId] or gLoadingCharacter[userId] then --存在或正在加载中，忽略
        return 
    end
    gLoadingCharacter[userId]=true
    PlayerCharacterHelper.GetPlayerCharacter(userId,function(character)
        gLoadingCharacter[userId]=nil

        if  character then
            character.Parent=gFolder
            character:PivotTo(gBroHideCframe)

            gCharacter[userId]= BroCharacterClass.new(character)
        else
            local reportStr=string.format("UserId:s% character==null",userId)
            warn(reportStr)
        end

    end)
end

function FindeCharacter(userId)
    return  gCharacter[userId]
end

function M.new(...)
	return M(...);
end;

function M.ctor(self,guid,userId,weaponId,level,isEquip)
    self.guiId=guid
    self.userId=userId
    self.weaponId=weaponId
    self.level=level
    self.isEquip=isEquip

    M.super.ctor(self);
    CreateCharacter(userId)
    self:LoadCharacter()
end;

function M.dtor(self)
    self:Hide()
	M.super.dtor(self);
end;

function M.ChangeWeapon(self,weaponId)
    self.weaponId=weaponId
    local character=FindeCharacter(self.userId)
    character:SetWeapon(self.weaponId)
end

local OutTime=60*2

function M.LoadCharacter(self)
    self:AddProcess(function()
        local startTime=os.time()
        local character=FindeCharacter(self.userId)
        while not character do
            character=FindeCharacter(self.userId)
            self:Wait(1)
            if os.time()-startTime>OutTime then
                return 
            end
        end
        self.character=character
        self.character.Name=tostring(self.userId)
        task.wait()
        if self.isShowing then
            self.character:SetWeapon(self.weaponId)
            self.character:Show(gBroShowCframe)
        end
    end)
end

function M.Show(self)
   
    self.isShowing=true
    if self.character then
        self.character:SetWeapon(self.weaponId)
        self.character:Show(gBroShowCframe)
    end
end;

function M.Hide(self)
    self.isShowing=nil
    if self.character then
        self.character:Hide(gBroHideCframe)
    end
end;

function M.Update(self,weaponId,level,isEquip)
    -- print("Update--->",self.guiId)

    if not self.character then
        self.weaponId=weaponId
        self.level=level
        self.isEquip=isEquip
        return
    end

    if self.level~=level then --升级
        self.character:Upgrade()
    elseif  self.weaponId~=weaponId  then --切换武器
    -- print("切换武器--->",self.guiId)
        self.character:ChangeWeapon(weaponId)
    elseif isEquip and not self.isEquip then --切换为装备
        self.character:Equip()
    end

    self.weaponId=weaponId
    self.level=level
    self.isEquip=isEquip
    
end;


return M